/*
 * 行列查碼程式的GUI介面
 * author:Blackdiz
 * date:2013/1/20
 * version:0.1beta
 * 
 * date:2013/1/26
 * comment:增加輸入欄位被點撃時會自動清空和顯示字碼區塊現在已經不能編輯
 * version:0.2beta
 */
package com.arrayutil.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.arrayutil.model.ArrayUtilM;

public class ArrayUtilV {

	private JFrame mainFrame;
	private JPanel upperPanel;
	private JPanel lowerPanel;
	private JTextArea codeArea; // 顯示字碼區塊
	private JButton queryButton;
	private JTextField wordField; // 輸入欲查詢的字的欄位
	private ArrayUtilM arrayUtilM = new ArrayUtilM();

	public ArrayUtilV() {
		mainFrame = new JFrame();
		upperPanel = new JPanel();
		lowerPanel = new JPanel();
		codeArea = new JTextArea(6, 6);
		queryButton = new JButton("查碼");
		wordField = new JTextField(4);

		upperPanel.add(wordField);
		upperPanel.add(queryButton);
		lowerPanel.add(codeArea);
		mainFrame.add(upperPanel, BorderLayout.NORTH);
		mainFrame.add(lowerPanel, BorderLayout.CENTER);
		mainFrame.setBounds(100, 200, 100, 200);
		mainFrame.setVisible(true);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		setComponent();
	}

	private void setComponent() {
		codeArea.setEditable(false);
		queryButton.addActionListener(new QueryListener());
		wordField.addKeyListener(new EnterListener());
		wordField.addFocusListener(new GetFocusListener());
	}

	class GetFocusListener extends FocusAdapter {
		@Override
		public void focusGained(FocusEvent e) {
			wordField.setText("");
		}
	}
	
	class QueryListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			codeArea.setText("");
			String queryWord = wordField.getText();
			List<String> code = arrayUtilM.queryCodes(queryWord);

			StringBuilder codeStrings = new StringBuilder();
			for (Iterator<String> codeI = code.iterator(); codeI.hasNext();) {
				codeStrings.append(codeI.next());
				codeStrings.append("\n");
			}

			codeArea.setText(codeStrings.toString());
		}
	}

	class EnterListener extends KeyAdapter {
		@Override
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				codeArea.setText("");
				String queryWord = wordField.getText();
				List<String> code = arrayUtilM.queryCodes(queryWord);

				StringBuilder codeStrings = new StringBuilder();
				for (Iterator<String> codeI = code.iterator(); codeI.hasNext();) {
					codeStrings.append(codeI.next());
					codeStrings.append("\n");
				}

				codeArea.setText(codeStrings.toString());
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new ArrayUtilV();
	}

}
